// https://leetcode.cn/problems/sliding-window-maximum/description/?envType=study-plan-v2&envId=top-100-liked

// 解法一优先级队列
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> ret;
        int n = nums.size();
        priority_queue<pair<int, int>> heap;
        for(int i = 0; i < k; i++) {
            heap.emplace(nums[i], i);
        }
        ret.push_back(heap.top().first);
        for(int i = k; i < n; i++) {
            heap.emplace(nums[i], i);
            while(heap.top().second <= i - k) {
                heap.pop();
            }
            ret.push_back(heap.top().first);
        }
        return ret;
    }
};

// 解法二单调队列
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> temp;
        deque<int> d;
        for(int i = 0; i < nums.size(); i++) {
            while(!d.empty() && d.front() < i - k + 1) {
                d.pop_front();
            }
            while(!d.empty() && nums[d.back()] < nums[i]) {
                d.pop_back();
            }
            d.push_back(i);
            if(i >= k - 1) {
                temp.push_back(nums[d.front()]);
            }
        }
        return temp;
    }
};